﻿
--#include "PkCommon.lua" once --pk配置

NOT_DROP_EXP  = 69	--禁止掉落经验地图属性
DROP_EXP_RATE = 70	--掉落经验倍率地图属性

NOT_ADD_EXP  = 71		--禁止获得经验地图属性
ADD_EXP_RATE = 72		--获得经验倍率地图属性

--被杀玩家掉落经验
function CalcOnKillDropExp(sysarg)
	if not PkConfig then return 0 end
	local pkExpConfig = PkConfig.pkExpParam
	local svar = Actor.getStaticVar(sysarg)	
	local data = svar.killerData	--击杀信息
	if not data then return 0 end
	local killerLevel  = data[1] 	--击杀者等级
	local killerCampId = data[2]	--击杀者阵营ID
	local killerName	 = data[3]	--击杀者名字
	local dropExpCount = data[4]	--每日PK经验掉落次数
	local killerType   = data[6]	--击杀者类型
	--print(dropExpCount)
	--print("data[1]:"..data[1])
	--print("data[2]:"..data[2])
	--print("data[3]:"..data[3])
	--print(data[4])
	--print(data[5])
	
	--判断击杀者类型
	if killerType ~= 0 then return 0 end
	
	if not killerName or killerName == "" then return 0 end
	
	--判断经验掉落次数
	if dropExpCount and dropExpCount > pkExpConfig.limitDropCount then return 0 end
	--print(dropExpCount)
	--判断击杀方最低等级
	if not killerLevel or killerLevel < pkExpConfig.killerLevel then return 0 end
	
	--判断被杀方最低等级
	local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, pkExpConfig.levelStart, 0)then return 0 end
	
	--判断地图属性是否禁止掉落经验
	if (Actor.hasMapAreaAttri(sysarg, NOT_DROP_EXP) == true) then return 0 end
	
	--最低经验限制
	local value = Actor.getIntProperty(sysarg, PROP_ACTOR_EXP)
	if value <= pkExpConfig.limitNum then return 0 end 
	
	--[[
	--掉落概率
	local percent = pkExpConfig.dropPercent[level - pkExpConfig.levelStart + 1]
	--print("percent:"..percent)
	if not percent or percent == 0 then return 0 end
	--判断概率
	local rand = math.random(1000)
	--print("rand:"..rand)
	if rand > percent then return 0 end 
	--]]
	
	--掉落经验值 = 经验标准值 * 对象参数 * 对象杀戮值参数 * 等级差距参数
	--掉落经验标准值
	local base = pkExpConfig.levelBase[level - pkExpConfig.levelStart + 1]
	if not base or base == 0 then return 0 end
	
	--print("base:"..base)
	--print(":"..)
	--根据对方和我的阵营关系，获得对象参数
	local otherParam = GetCampRelationParam(sysarg, killerCampId, pkExpConfig.otherParam)
	if not otherParam or otherParam == 0 then return 0 end
	--print("otherParam:"..otherParam)
	--根据己方的杀戮值计算杀戮值参数
	local pkValue = Actor.getIntProperty(sysarg, PROP_ACTOR_PK_VALUE)
	local pkParam = GetOtherParam(pkExpConfig.otherPk, pkValue)
	if not pkParam or pkParam == 0 then return 0 end
	--print("pkParam:"..pkParam)
	--根据等级差距获得参数
	local levelParam = GetOtherParam(pkExpConfig.levelGap, killerLevel-level)
	if not levelParam or levelParam == 0 then return 0 end
	--print("levelParam:"..levelParam)
	
	--计算掉落经验值 = 经验标准值 * 对象参数 * 对象杀戮值参数 * 等级差距参数
	local nValue = base*otherParam*pkParam*levelParam
	
	local nRate = Actor.getMapAttriValue(sysarg, DROP_EXP_RATE)
	if nRate > 0 then
		nValue = math.floor(nValue * (nRate/1000))
	end
	--print(nRate)
	local nVal = value - nValue
	if nVal < pkExpConfig.limitNum then 
		nValue = value - pkExpConfig.limitNum 
	end
	--local dropValue = math.min(value, nValue)
	local dropValue = math.floor(math.abs(nValue))
	if dropValue <= 0 then return 0 end 
	--print(dropValue)
	--[[
	if Actor.giveAward(sysarg, 2,0, -dropValue, 0, 0, 1, 0,0,  "OnKillDropExp") == true then
		if not dropExpCount then dropExpCount = 0 end
		data[4] = dropExpCount + 1
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.pk00001, killerName, dropValue), ttTipmsgWindow)
		return dropValue
	end
	--]]
	return dropValue
end


--击杀玩家获得经验
--nValue 获得的经验值
function OnKillGetExp(sysarg, nValue)
	if not PkConfig then return end
	if not nValue or nValue <= 0 then return end
	local pkExpConfig = PkConfig.pkExpParam
	local svar = Actor.getStaticVar(sysarg)	
	local data = svar.killerData	--击杀信息
	if not data then return end
	local killerLevel  = data[1] 	--击杀者等级
	local killerCampId = data[2]	--击杀者阵营ID
	local killerName	 = data[3]	--击杀者名字
	--print("data[1]:"..data[1])
	--print("data[2]:"..data[2])
	
	--print("nValue:"..nValue)
	if not killerName or killerName == "" then return end
	
	local killerPtr = Actor.getActorPtrByName(killerName)
	if not killerPtr then return end
	
	local killerSvar = Actor.getStaticVar(killerPtr)
	local killerData = killerSvar.killerData
	local getExpCount	 = killerData[5]	--每日PK经验获得次数
	--print(getExpCount)
	--判断经验获得次数
	if getExpCount and getExpCount > pkExpConfig.limitGetCount then return end
	--print(getExpCount)
	
	--判断击杀方最低等级
	if not killerLevel or killerLevel < pkExpConfig.killerLevel then return end
	
	--判断被杀方最低等级
	--local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, pkExpConfig.levelStart, 0) then return end
	
	local beKillId = Actor.getIntProperty(sysarg, PROP_ENTITY_ID)
	local curTime = System.getCurrMiniTime()
	local killTime = Actor.getPkKillerTimeData(killerPtr, beKillId, 2)
	--print(killTime)
	if curTime < killTime + pkExpConfig.killInterval then 
		return
	end
	
	--判断地图属性是否禁止掉落经验
	if (Actor.hasMapAreaAttri(sysarg, NOT_ADD_EXP) == true) then return end
	
	--经验倍率
	local rate = pkExpConfig.levelRate[killerLevel - pkExpConfig.killerLevel + 1]
	if not rate or rate == 0 then return end
	
	--计算获取的经验值 = 被杀方掉落经验值 * 击杀方当前等级对应经验倍率
	local addValue = nValue * rate
	
	--经验上限
	local limitExp = pkExpConfig.limitExp[killerLevel - pkExpConfig.killerLevel + 1]
	if limitExp > 0 and addValue > limitExp then 
		addValue = limitExp
	end
	
	
	local nRate = Actor.getMapAttriValue(sysarg, ADD_EXP_RATE)
	if nRate > 0 then
		addValue = math.floor(addValue * (nRate/1000))
	end
	--print(nRate)
	--local addValue = math.min(value, nValue)
	local addValue = math.floor(math.abs(addValue))
	if addValue <= 0 then return end 
	--print(addValue)
	
	if Actor.setPkKillerTimeDate(killerPtr, beKillId, 2, curTime) == true then
		if Actor.giveAward(killerPtr, 2, 0,addValue, 0, 0, 1, 0,0,  "OnKillGetExp") == true then
			if not getExpCount then getExpCount = 0 end
			killerData[5] = getExpCount + 1
			local killerCamp = Camp.getCampName(Actor.getCamp(killerPtr))
			local killerName = Actor.getName(killerPtr)
			local beKillCamp = Camp.getCampName(Actor.getCamp(sysarg))
			local beKillName = Actor.getName(sysarg)
			if addValue >= pkExpConfig.noticeLimitExp then
				System.broadcastTipmsg(string.format(Lang.ScriptTips.pk00002,killerCamp,killerName,beKillCamp,beKillName,beKillName), ttScreenCenter)
			end
			Actor.sendTipmsg(killerPtr, string.format(Lang.ScriptTips.pk00003, beKillName, addValue), ttTipmsgWindow)
		end
	end
end